<?php

/**

 * @authors alex (20779512@qq.com)

 * @date    2014-04-15 08:58:26

 * @version 1.0

 */

namespace Admin\Controller;



class CategoryController extends AdminController {

    

    Public $arr = array();



	public function _initialize(){    	



		parent::_initialize();



		$where['user'] = array('like','%'.$this->adminName.'%');

		$list = M('Category')->field('id')->where($where)->select();

		foreach ($list as $key => $value) {

			$this->arr[] = $value['id'];

			$this->getClassID($value['id'],$arr);

		}



	}



    public function getClassID($fid,$arr){

		$map['fid']=$fid;

		$list = M('Category')->field('id')->where($map)->select();

		foreach ($list as $key => $value) {			

			$this->arr[] = $value['id'];

			$this->getClassID($list['id'],$arr);

		}

	}



	#文章分类列表

	function index() {

		$artClass = M('Category');

		$list = $artClass->field("id,name,user,fid,path,sort")->order('path,id asc')->select();

		foreach ($list as $key => $value) {

			$count = count(explode('-', $value['path'])) - 2;

			if ($value['fid'] > 0) {

				$list[$key]['style'] = 'style="padding-left:' . (($count * 10) + 10) . 'px;"';

			}

		}



		$this->assign('list', $list);

		$this->display();

	}

	

	/*

	 *分类部分的增删改操作

	 */

 	function add(){



		$fid=I('get.fid',0);

		$path=I('get.path','0-');



 		$artClass = M('Category');



		$list = $artClass->field("id,name,fid,path")->order('path')->select();



		foreach ($list as $key => $value) {

			$count = count(explode('-', $value['path'])) - 3;

			$list[$key]['count'] = $count;



		}



		$this->assign('list', $list);

		$this->assign('fid', $fid);

		$this->assign('path', $path);

		$this->display();

 	}





	function edit(){



		$id = I('get.id');

		if(isset($id)){

			$artClass = M('Category');

			$clist = $artClass->field("id,name,fid,path")->order('path')->select();

			foreach ($clist as $key => $value) {

			$count = count(explode('-', $value['path'])) - 3;

			$clist[$key]['count'] = $count;

			}

			$cate = M('Category');

			$list=$cate->where('id='.$id)->find();

			if($list){

				$this->assign('list',$list);

				$this->assign('clist', $clist);

				$this->display();

			}else{

				$this->error('There is no such classification');

			}

		}else{

			$this->error('Parameter error');

		}

	}





	function save(){



		$cate = D('Category');



		if($date = $cate->create()){



			if ($list=$cate->add($date)) {



				$date['path']=$date['path'].$list.'-';



				$cate->where('id='.$list)->save($date);



				$state = 'SUCCESS';

                $message = 'Successful operation';

                $url = U('Category/index');



			} else {

				$state = 'Operation failed';

			}



		}else{

			$state = $cate->getError();

		}

		echo $this->echo_json_str($state,$message,$url);

	}





	function update(){



		$cate = D('Category');



		if ($date = $cate->create()) {



			if($date['id']==$date['fid']){

				$state = 'Can not take oneself as superior column';

				echo $this->echo_json_str($state,$message,$url);

				die;

			}



			$thisFID = $_POST['thisFID'];



			if($thisFID==0 && $date['fid']>0){

				$state = 'Root column can not move';

				echo $this->echo_json_str($state,$message,$url);

				die;

			}



			if($date['fid']>0){



				$tClass = M('Category');



				$fdate = $tClass->field('path')->where('id='.$date['fid'])->find();



				if(strstr($fdate['path'],$date['path'])){



					$state = 'Can not move to its lower column';

					echo $this->echo_json_str($state,$message,$url);

					die;

				}

				$date['path']=$fdate['path'].$date['id'].'-';

			}else{

				$date['path']=I('post.path');

			}



			$oldPath = I('post.path');



			if ($cate->save($date)) {



				$cate->execute("UPDATE __PREFIX__category SET path = replace(path, '".$oldPath."','".$date['path']."')");

				$state = 'SUCCESS';

				$message = 'Successful operation';

				$url = U('Category/index');



			} else {

				$state = 'Operation failed';

			}



		} else {

			$state = $cate->getError();

		}



		echo $this->echo_json_str($state,$message,$url);



	}





	function del(){



		$id = I('get.id');

		if(!isset($id)){

		$this->error("You do not choose any category");

		}



		$cate = M('Category');



		$list = $cate->where('fid='.$id)->find();



		if($list){

			echo '2';die;

		}



		$list = $cate->where('id='.$id)->delete();



		$article=M('Article');



		$article->where('cid='.$id)->delete();



		if($list){

			echo '1';

		}else{

			echo '0';

		}



	}

}